<script setup>
import { ref, onMounted } from "vue";
import { queryPageApi, queryAllDeptsApi } from "@/api/user";
import { ElMessage } from "element-plus";

// onMounted
onMounted(() => {
  search();
  loadDepts(); //此处是部门下拉菜单栏显示的,应该在页面加载的时候获取
});

// 查询参数
const queryParams = ref({
  name: "",
  phone: "",
  deptId: "",
  status: "",
  page: 1,
  pageSize: 10,
});

// 表格数据
const userList = ref([]);
const total = ref(0);

// 部门列表
const deptList = ref([]);

// 加载部门列表
const loadDepts = async () => {
  const result = await queryAllDeptsApi();
  if (result.code === 1) {
    deptList.value = result.data;
  }
};

// 搜索
const search = async () => {
  const result = await queryPageApi(queryParams.value);
  if (result.code === 1) {
    userList.value = result.data.rows;
    total.value = result.data.total;
  }
};

// 清空
const clear = () => {
  queryParams.value = {
    name: "",
    phone: "",
    deptId: "",
    status: "",
    page: 1,
    pageSize: 10,
  };
  search();
};

// 分页大小改变
const handleSizeChange = (val) => {
  search();
};

// 页码改变
const handleCurrentChange = (val) => {
  search();
};

// 获取性别显示文本
const getGenderText = (gender) => {
  return gender === 1 ? "男" : gender === 2 ? "女" : "";
};

// 获取状态显示文本
const getStatusText = (status) => {
  return status === 1 ? "正常" : "停用";
};

// 获取状态样式类
const getStatusClass = (status) => {
  return status === 1 ? "status-normal" : "status-disabled";
};
</script>

<template>
  <div class="user-management">
    <!-- 搜索区域 -->
    <el-form
      :inline="true"
      :model="queryParams"
      ref="queryForm"
      class="search-form"
    >
      <el-form-item label="姓名:">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机号:">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="部门:">
        <el-select
          v-model="queryParams.deptId"
          placeholder="请选择"
          clearable
          style="width: 100%"
        >
          <el-option
            v-for="dept in deptList"
            :key="dept.id"
            :label="dept.name"
            :value="dept.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态:">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          style="width: 100%"
        >
          <el-option label="正常" :value="1" />
          <el-option label="停用" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item class="search-buttons">
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button @click="clear">清空</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <div class="operation-area">
      <el-button type="primary">+ 添加用户</el-button>
      <el-button>批量删除</el-button>
    </div>

    <!-- 表格区域 -->
    <el-table :data="userList" style="width: 100%" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="name" label="姓名" width="120" align="center" />
      <el-table-column
        prop="username"
        label="用户名"
        width="120"
        align="center"
      />
      <el-table-column label="头像" width="100" align="center">
        <template #default="scope">
          <el-avatar :size="40" :src="scope.row.image" :alt="scope.row.name" />
        </template>
      </el-table-column>
      <el-table-column
        prop="deptName"
        label="部门"
        width="150"
        align="center"
      />
      <el-table-column
        prop="roleName"
        label="角色"
        width="150"
        align="center"
      />
      <el-table-column prop="phone" label="手机号" width="150" align="center" />
      <el-table-column label="状态" width="100" align="center">
        <template #default="scope">
          <span :class="getStatusClass(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="最后操作时间"
        width="180"
        align="center"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link> 修改 </el-button>
          <el-button type="danger" link> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 40, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<style scoped>
.user-management {
  padding: 20px;
  background-color: #fff;
  min-height: 100%;
  border-radius: 8px;
}

.operation-area {
  margin: 20px 0;
}

.search-form {
  display: flex;
  align-items: center;
}

.search-form :deep(.el-form-item) {
  margin-right: 20px;
  min-width: 200px;
}

.search-buttons {
  margin-left: auto !important;
  min-width: auto !important;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.status-normal {
  color: #000;
}

.status-disabled {
  color: #f56c6c;
}

:deep(.el-button .el-icon) {
  margin-right: 4px;
}
</style>
